﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeedTest
{
	public partial class AdminRelease : Form
	{
		private Data data = null;
		EditMode mode = EditMode.EditMode;

		public AdminRelease(Data data)
		{
			InitializeComponent();
			this.data = data;
		}

		private void AdminRelease_Load(object sender, EventArgs e)
		{
			lstReleases.ValueMember = "Value";
			lstReleases.DisplayMember = "Text";

			DataTable dt = data.GetReleases();
			FillList(dt, dt.Columns["ID"].Ordinal, dt.Columns["Description"].Ordinal, lstReleases);
			ResetListSelection();
			EnableInput(false);
		}

		private void FillList(DataTable table, int valueIndex, int textIndex, ListBox list)
		{
			if (table != null && table.Rows.Count > 0)
			{
				list.Items.Clear();

				for (int i = 0; i < table.Rows.Count; i++)
				{
					ComboItem ci = new ComboItem();
					ci.Text = table.Rows[i][textIndex].ToString();
					ci.Value = table.Rows[i][valueIndex].ToString();

					list.Items.Add(ci);
				}
			}
		}

		private void lstReleases_SelectedIndexChanged(object sender, EventArgs e)
		{
			LoadSelectedItem();
		}

		private void LoadSelectedItem()
		{
			ComboItem ci = (ComboItem)lstReleases.SelectedItem;
			DataTable table = data.GetReleaseByID(ci.Value);

			if (table != null && table.Rows.Count > 0)
			{
				txtDescription.Text = table.Rows[0]["Description"].ToString();
				pckExpirationDate.Value = DateTime.Parse(table.Rows[0]["ExpirationDate"].ToString());
				bool isActive = Boolean.Parse(table.Rows[0]["Active"].ToString());
				chkActive.Checked = isActive;
			}
		}

		private void EnableInput(bool enable)
		{
			txtDescription.Enabled = enable;
			pckExpirationDate.Enabled = enable;
			chkActive.Enabled = enable;
			btnSave.Enabled = enable;
			btnCancel.Enabled = enable;

			if (IsPermitted(btnEdit, data.User.Releases)) { btnEdit.Enabled = !enable; }
			else { btnEdit.Enabled = false; }
			if (IsPermitted(btnAdd, data.User.Releases)) { btnAdd.Enabled = !enable; }
			else { btnAdd.Enabled = false; }
			if (IsPermitted(btnRemove, data.User.Releases)) { btnRemove.Enabled = !enable; }
			else { btnRemove.Enabled = false; }
			lstReleases.Enabled = !enable;

        }

		private bool IsPermitted(Button btn, Permissions perm)
		{
			if (btn == btnRemove) { return perm.Delete; }
			if (btn == btnAdd) { return perm.Create; }
			if (btn == btnEdit) { return perm.Edit; }

			return false;
		}

		private void ResetListSelection()
		{
			if (lstReleases.Items.Count > 0)
			{
				lstReleases.SelectedIndex = 0;
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			if (lstReleases.SelectedItem != null)
			{
				EnableInput(true);
				txtDescription.Focus();
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			ComboItem ci = (ComboItem)lstReleases.SelectedItem;
			DataTable table = new DataTable();

			table.Columns.Add("Description");
			table.Columns.Add("ExpirationDate");
			table.Columns.Add("Active");

			string[] row = new string[3];
			row[0] = txtDescription.Text;
			row[1] = pckExpirationDate.Value.ToString("yyyy-MM-dd");
			
			if(ci.Value.Equals("1"))	//The Library release - is required
			{
				row[2] = "TRUE";
			}
			else{row[2] = chkActive.Checked ? "TRUE" : "FALSE";}

			table.Rows.Add(row);

			if (mode == EditMode.EditMode)
			{
				data.UpdateReleaseByID(table, ci.Value);
			}
			else
			{	//create mode
				data.InsertRelease(table);
				//reset mode back to default
				mode = EditMode.EditMode;
			}

			DataTable dt = data.GetReleases();
			FillList(dt, dt.Columns["ID"].Ordinal, dt.Columns["Description"].Ordinal, lstReleases);
			ResetListSelection();
			EnableInput(false);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			LoadSelectedItem();
			mode = EditMode.EditMode;	//reset to default
			ResetListSelection();
			EnableInput(false);
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			mode = EditMode.CreateMode;
			txtDescription.Text = String.Empty;
			pckExpirationDate.Value = DateTime.Now;
			chkActive.Checked = true;

			EnableInput(true);
			txtDescription.Focus();
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{
			if (lstReleases.SelectedItem != null)
			{
				ComboItem ci = (ComboItem)lstReleases.SelectedItem;
				if (ci.Value.Equals("1")) 
				{ 
					MessageBox.Show("This release cannot be deleted");
					return;
				} //The Library release cannot be deleted.

				DialogResult res = MessageBox.Show("Are you sure you want to delete the item: " + ci.Text +
					System.Environment.NewLine +
					"This action cannot be undone." +
					System.Environment.NewLine +
					"All test plans, cases and suites associated with this release will be deleted.", "Confirm Deletion", MessageBoxButtons.YesNo);

				if (res == DialogResult.Yes)
				{
					data.DeleteReleaseByID(ci.Value);
					DataTable dt = data.GetReleases();
					FillList(dt, dt.Columns["ID"].Ordinal, dt.Columns["Description"].Ordinal, lstReleases);
					ResetListSelection();

					//TODO: DELETE PLANS, CASES, SUITES, RESULTS, ATTACHMENTS FOR THE RELEASE
				}
			}
		}
	}
}
